<?php 

class giftController extends appController
{
    
    /**
     * 显示游戏配置信息
     */
    public function gameConfig()
    {
        $gift = Model('gift');
        
        $id = yf_empty(intval($_GET['id']), 0, true);
        $info = $gift->getGameConfig($id);
        $this->assign('info', $info);
        $this->assign('action', 'editConfig');
        $this->display('config.html');
    }
    
    /**
     * 修改游戏配置
     */
    public function editConfig()
    {
        $gift = Model('gift');
        $res = array('error' => 1, 'content' => '修改失败');
        $id = yf_empty(intval($_POST['id']), '参数错误');
        $info['count_type'] = yf_empty(intval($_POST['count_type']), 0, true);
        $info['game_count'] = yf_empty(intval($_POST['game_count']), 0, true);
        if($info['count_type'] > 0 && $info['game_count'] <= 0)
        {
            $res['content'] = '请输入游戏次数';
            $this->showMsg($res);
        }
        
        $info['is_share'] = yf_empty(intval($_POST['is_share']), 0, true);
        $info['share_count'] = yf_empty(intval($_POST['share_count']), 0, true);
        $info['share_type'] = yf_empty(intval($_POST['share_type']), 1, true);
        $info['max_share'] = yf_empty(intval($_POST['max_share']), 0, true);
        if($info['is_share'] > 0 && $info['share_count'] <= 0)
        {
            $res['content'] = '请输入分享后增加的游戏次数';
            $this->showMsg($res);
        }
        
        $info['add_type'] = yf_empty(intval($_POST['add_type']), 0, true);
        
        $info['limit_status'] = yf_empty(intval($_POST['limit_status']), 1, true);
        $info['limit_time'] = yf_empty(intval($_POST['limit_time']), 1800, true);
        $info['limit_count'] = yf_empty(intval($_POST['limit_count']), 0, true);
        if($info['limit_status'] == 1 && $info['limit_count'] <= 0)
        {
            $res['content'] = '请输入限制数量';
            $this->showMsg($res);
        }
        $result = $gift->updateConfig($info, $id);
        if($result)
        {
            $res = array('error' => 0, 'content' => '修改成功');
        }
        $this->showMsg($res);
    }
    
    /**
     * 显示游戏记录
     */
    public function gameLog()
    {
        $gift = Model('gift');
        $page = yf_empty(intval($_GET['page']), 1, true);
        $list = $gift->getJoinLog($page);
        $this->assign('list', $list['result']);
        $this->assign('page', $list['page']);
        
        $allCount = $gift->getCountJoin();
        $daoLimit = 1000;
        $num = ceil($allCount / $daoLimit);
        $arr = array();
        if($num > 0)
        {
            $start = 1;
            for($i = 0; $i < $num; $i++)
            {
            $arr[$i]['num'] = $i + 1;
            $arr[$i]['start'] = $start;
            $arr[$i]['end'] = $start + $daoLimit - 1;
            $start += $daoLimit;
            }
            }
            $this->assign('arr', $arr);
        $this->display('gameLog.html');
    }
    
    /**
     * 显示详情
     */
    public function detail()
    {
        $gift = Model('gift');
        $id = yf_empty(intval($_GET['id']), '参数错误');
        $num = yf_empty(intval($_GET['num']), '参数错误');
        $info = $gift->getOneInfo($id);
        $this->assign('info', $info);
        $this->assign('num', $num);
        $this->display('detail.html');
    }
    
    /**
     * 记录详情
     */
    public function detailLog()
    {
        $gift = Model('gift');
        $id = yf_empty(intval($_GET['id']), '参数错误');
        $page = yf_empty(intval($_GET['page']), 1, true);
        $list = $gift->getOneLog($id, $page);
        $this->assign('list', $list['result']);
        $this->assign('page', $list['page']);
        $this->display('detailLog.html');
    }
    
    /**
     * 加分记录
     */
    public function addLog()
    {
        $gift = Model('gift');
        $page = yf_empty(intval($_GET['page']), 1, true);
        $wxid = yf_empty(intval($_GET['wxid']), '参数错误');
        $openid = yf_empty($_GET['openid'], '参数错误');
        $list = $gift->getAddLog($wxid, $openid, $page);
        $this->assign('list', $list['result']);
        $this->assign('page', $list['page']);
        $this->display('addLog.html');
    }
    
    /**
     * 导出数据
     */
    public function daoLog()
    {
        $page = yf_empty(intval($_POST['num']), 0, true);
        $limit = 1000;
        $gift = Model('gift');
        $info = $gift->getDaoLog($page, $limit);
        if($info)
        {
            $table = array('排名', '昵称', 'openid', '总分数', '总次数', '正常分数', '正常次数', '分享分数', '分享次数', '加分', '无效分', '最后时间', '最后IP');
            yf_daochu($table, $info, date('YmdHi', yf_time())."记录");
        }
        $this->gameLog();
    }
}